package edu.cibertec.alquiler.dao.persistence;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import com.sun.istack.internal.FinalArrayList;

import edu.cibertec.alquiler.dao.VideoDAO;
import edu.cibertec.alquiler.entity.DetalleUsuarioVideo;
import edu.cibertec.alquiler.entity.Video;

public class VideoJPADAO implements VideoDAO {
	
	private EntityManagerFactory emf;
	private EntityManager em;
	
	
	public void setEntityManagerFactory(EntityManagerFactory emf){
			this.emf=emf;
		}

	@SuppressWarnings("unchecked")
	@Override
	public List<Video> listarVideos() throws Exception {
		em=emf.createEntityManager();
		List<Video> listaVideos=new ArrayList<Video>();
		try {
			Query q = em.createQuery("Select v from Video v");
			listaVideos= q.getResultList();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally{
			em.clear();
			em.close();
		}
		return listaVideos;
	}

	@Override
	public void insertarNuevoVideo(Video bean) throws Exception {
		em=emf.createEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(bean);
			em.flush();
			em.getTransaction().commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
			em.getTransaction().rollback();
		}finally{
			em.clear();
			em.close();
		}
	}

	@Override
	public void nuevoPrestamo(DetalleUsuarioVideo bean) throws Exception {
		em= emf.createEntityManager();
		try {
			em.getTransaction();
			em.persist(bean);
			em.flush();
			em.getTransaction().commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally{
			em.clear();
			em.close();
		}
	}

	@Override
	public Video buscarVideo(Video bean) throws Exception {
		em=emf.createEntityManager();
		Video entidad=null;
		try {
			 entidad= em.find(Video.class, bean.getCodVideo());
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally{
			em.clear();
			em.close();
		}
		return entidad;
	}

	@Override
	public void actualizarVideo(Video bean) throws Exception {
		em=emf.createEntityManager();
		Video video=null;
		try {
			em.getTransaction().begin();
			video=em.find(Video.class, bean.getCodVideo());
			
			video.setAnio(bean.getAnio());
			video.setDirector(bean.getDirector());
			video.setDuracion(bean.getDuracion());
			video.setPrecio(bean.getPrecio());
			video.setProtagonista(bean.getProtagonista());
			video.setSinopsis(bean.getSinopsis());
			video.setStock(bean.getStock());
			video.setTbCategoria(bean.getTbCategoria());
			video.setTbEstado(bean.getTbEstado());
			video.setTbGenero(bean.getTbGenero());
			video.setTitulo(bean.getTitulo());
			video.setUrlImagen(bean.getUrlImagen());
			video.setUrlTrailer(bean.getUrlTrailer());
			em.merge(video);
			em.flush();
			em.getTransaction().commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally{
			em.clear();
			em.close();
		}
	}

	@Override
	public void eliminarVideo(Video bean) throws Exception {
		em=emf.createEntityManager();
		Video video=null;
		try {
			video=em.find(Video.class, bean.getCodVideo());
			em.remove(video);
			em.flush();
			em.getTransaction().commit();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally{
			em.clear();
			em.close();
		}
		
		
	}
}
